package models.contract;

import models.UUIDModel;
import models.accesscard.AccessCard;
import models.constants.DeletedStatus;
import models.user.User;
import play.db.jpa.JPA;
import util.common.UStringUtil;

import javax.persistence.*;
import java.util.List;

/**
 * 合同  门禁卡 微信用户关联
 */
@Entity
@Table(name = "co_contract_card_relations")
public class ContractCardRelation extends UUIDModel {

    /**
     * 合同信息
     */
    @JoinColumn(name = "contract_id")
    @ManyToOne
    public Contract contract;

    /**
     * 门禁卡信息
     */
    @JoinColumn(name = "access_cards")
    @ManyToOne
    public AccessCard accessCard;

    /**
     * 关联微信用户
     */
    @JoinColumn(name = "user_id")
    @ManyToOne
    public User user;


    /**
     * 绑定门禁卡的用户名称
     */
    @Column(name = "name")
    public String name;

    /**
     * 绑定门禁卡人联系电话
     */
    @Column(name = "phone")
    public String phone;


    /**
     * 身份证编号
     */
    @Column(name = "identity_card")
    public String identityCard;


    /**
     * 根据 code 查找卡号信息
     *
     * @param code
     * @return
     */
    public static ContractCardRelation findByCardNumber(String code) {
        return ContractCardRelation.find("accessCard.code =? and deleted =?" , code , DeletedStatus.UN_DELETED).first();
    }


    /**
     * 根据 user 查找卡号信息
     * @param user
     * @return
     */
    public static ContractCardRelation findByUser(User user) {
        return ContractCardRelation.find("user =? and deleted =?" , user , DeletedStatus.UN_DELETED).first();
    }

    public static List<ContractCardRelation> findAllByUser(User user) {
        return ContractCardRelation.find("user = ? and deleted = ?",user,DeletedStatus.UN_DELETED).fetch();
    }

    public static ContractCardRelation findByUserAndCode(User user,String code) {
        return ContractCardRelation.find("user = ? and contract.code = ? and deleted = ? ",user,code,DeletedStatus.UN_DELETED).first();
    }

    /**
     * 根据Id查询
     *
     * @param id
     * @return
     */
    public static ContractCardRelation findByContractCardRelationId(String id) {
        return ContractCardRelation.find(" id = ? and deleted = ?", id, DeletedStatus.UN_DELETED).first();
    }

    /**
     * 删除
     *
     * @param ids
     */
    public static void deleteContractCardRelation(String[] ids) {
        String idStr = "'" + UStringUtil.concatStr("','", ids) + "'";
        String sqlUpdate = " update co_contract_card_relations a set a.deleted = 1 where a.id in (" + idStr + ")";
        JPA.em().createNativeQuery(sqlUpdate).executeUpdate();
    }

    /**
     * 删除合同同时删除绑定的门禁卡
     * @param contractId
     */
    public static void deleteContractCardRelations(String contractId) {
        String sqlUpdate = " update co_contract_card_relations a set a.deleted = 1 where a.contract_id = '"+contractId+"'";
        JPA.em().createNativeQuery(sqlUpdate).executeUpdate();
    }

    /**
     * 查询某合同下的门禁卡信息
     *
     * @param contractId
     * @return
     */
    public static List<ContractCardRelation> loadByContractId(String contractId) {
        return ContractCardRelation.find(" contract.id = ? and deleted = ? order by name desc,createdAt desc", contractId, DeletedStatus.UN_DELETED).fetch();
    }

}
